package com.icss.train_sys_back.service.home.impl;

import com.icss.train_sys_back.config.RabbitMQConfig;
import com.icss.train_sys_back.entity.order.OrderMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OrderMessageConsumer {

    /**
     * 监听订单队列并处理异步任务
     */
    @RabbitListener(queues = RabbitMQConfig.ORDER_QUEUE)
    public void processOrderMessage(OrderMessage orderMessage) {
        log.info("开始处理订单异步任务: {}", orderMessage);
        try {
            // 1. 实际业务处理逻辑（示例：更新订单状态、发送通知等）
            // orderService.updateOrderStatus(orderMessage.getOrderId(), OrderStatus.PROCESSING);
            // notificationService.sendOrderCreatedMsg(orderMessage.getUserId());

            // 2. 模拟处理延迟
            Thread.sleep(1000);
            log.info("订单异步任务处理完成: {}", orderMessage.getOrderId());
        } catch (Exception e) {
            log.error("订单异步处理失败", e);
            // 可配置重试机制或发送到死信队列
            throw new RuntimeException("订单处理失败", e);
        }
    }
}